package dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import dto.Sample;

public class SampleLoader {
	
	private static int getIntK(String str) {
		int result;
		if(str.length() == 1) {
			result = str.toCharArray()[0] - '0';
		} else {
			result = 10 + (str.toCharArray()[1] - '0');
		}
		return result;
	}
	
	private static int getInt(String str) {
		return str.toCharArray()[0] - '0';
	}
	
	public static List<Sample> getSamples(String filename, String labelfile) {
		List<String> lines = FileIO.readFile(filename);
		lines = lines.subList(1, lines.size());
		List<Sample> samples = new ArrayList<Sample>(lines.size());
		
		for(String line : lines) {
			String [] tokens = line.split("\\s");
			Sample sample = new Sample();
			sample.setPredicted(getInt(tokens[0]));
			sample.setK(getIntK(tokens[3]));
			
			int layer = tokens.length - 5;
			int [] lr = new int[layer];
			for(int i = 0; i < layer; i++) {
				lr[i] = getInt(tokens[i + 5]);
			}
			sample.setLayers(lr);
			samples.add(sample);
		}
		
		List<String> labels = FileIO.readFile(labelfile);
		Iterator<Sample> itr = samples.iterator();
		for(String label : labels) {
			Sample sample = itr.next();
			sample.setLabel(getInt(label));
		}
		
		
		
		return samples;
	}
}
